import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/index'
}, {
  path: '/index',
  redirect: 'index/chat',
  component: () => import('../views/Index.vue'),
  children: [{
    path: 'chat',
    component: () => import('../views/Index/Chat.vue')
  }, {
    path: 'mailList',
    component: () => import('../views/Index/MailList.vue')
  }, {
    path: 'setting',
    component: () => import('../views/Index/Setting.vue')
  }]
}, {
  path: '/login',
  component: () => import('../views/Login.vue')
}, {
  path: '/register',
  component: () => import('../views/Register.vue')
}, {
  path: '*',
  component: () => import('../views/404.vue')
}]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isToken = localStorage.token ? true : false
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    isToken ? next() : next('/login')
  }
})

export default router